package com.woniuxy.sale.controller;


import com.woniuxy.model.vo.UpdateSaleMoneyVo;
import com.woniuxy.response.CustomPage;
import com.woniuxy.response.ResponseResult;
import com.woniuxy.model.entity.ExportTable;
import com.woniuxy.model.entity.SaleMoney;
import com.woniuxy.sale.entity.dto.SaleMoneyDTO;
import com.woniuxy.sale.entity.vo.*;
import com.woniuxy.sale.exception.SaleMoneyAddException;
import com.woniuxy.sale.service.SaleMoneyService;
import com.woniuxy.sale.util.MyExcelUtils;
import com.woniuxy.util.CheckTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 金额结算表; 前端控制器
 * </p>
 *
 * @author lio
 * @since 2023-09-07
 */
@Slf4j
@RestController
@RequestMapping("/saleMoney")
public class SaleMoneyController {

    @Resource
    private SaleMoneyService saleMoneyService;

    /**
     * 填写金额结算单
     * 前端传入:sno type totalPrice
     * 填写金额结算表 类型:定金/尾款 选货单编号 总价
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody SaleMoneyVo saleMoneyVo, HttpServletRequest request) throws SaleMoneyAddException {

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String operator = (String) user.get("account");
        saleMoneyVo.setOperator(operator);
        saleMoneyVo.setCreator(operator);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",saleMoneyService.add(saleMoneyVo));
    }

    /**
     * 修改金额结算表状态
     */
    @PostMapping("/updateState2")
    public ResponseResult<Boolean> updateState2(@RequestBody UpdateSaleMoneyVo updateSaleMoneyVo){

        if(saleMoneyService.updateState(updateSaleMoneyVo)){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }
        return new ResponseResult<>(ResponseResult.UpdateSaleOrderState_ERROR,"fail",false);
    }

    /**
     * 财务管理模块:客户账单管理 current、size、customer
     */
    @PostMapping("/moneyManage")
    public ResponseResult<CustomPage<List<SaleMoney>>> moneyManage(@RequestBody MoneyManageVo moneyManageVo){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",saleMoneyService.moneyManage(moneyManageVo));
    }

    /**
     * 删除未提交的金额结算表
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @DeleteMapping("/del/{id}")
    public ResponseResult<Boolean> del(@PathVariable("id") Integer id, HttpServletRequest request){

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String operator = (String) user.get("account");

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",saleMoneyService.del(id,operator));
    }

    /**
     * 提交结算单 id
     * 1.修改结算单状态
     * 2.填写account_manage表 客户名称 应收金额。。。
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @PostMapping("submitSaleMoney")
    public ResponseResult<Boolean> submitSaleMoney(@RequestBody SubmitSaleMoneyVo submitSaleMoneyVo, HttpServletRequest request){

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String operator = (String) user.get("account");
        submitSaleMoneyVo.setOperator(operator);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",saleMoneyService.submitSaleMoney(submitSaleMoneyVo));
    }

    /**
     * 修改金额结算表 只有制单人才能修改
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @PostMapping("/updateSaleMoney")
    public ResponseResult<Boolean> updateSaleMoney(@RequestBody SaleMoneyUpdateVo saleMoneyUpdateVo,HttpServletRequest request){

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String operator = (String) user.get("account");
        saleMoneyUpdateVo.setOperator(operator);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",
                saleMoneyService.updateSaleMoney(saleMoneyUpdateVo));
    }

    /**
     * 金额结算单主页
     */
    @PostMapping("moneyInfo")
    public ResponseResult<CustomPage<List<SaleMoneyDTO>>> moneyInfo(@RequestBody SaleMoneyInfoVo saleMoneyInfoVo){

        log.debug(saleMoneyInfoVo.toString());

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",saleMoneyService.moneyInfo(saleMoneyInfoVo));
    }

    /**
     * 导出成excel
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @PostMapping("exportSaleMoney")
    public void exportSaleMoney(@RequestBody SaleMoneyInfoVo saleMoneyInfoVo, HttpServletResponse response,HttpServletRequest request) {

        List<SaleMoneyDTO> list = saleMoneyService.moneyInfo(saleMoneyInfoVo).getData();
        log.debug(list.toString());

        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String account = (String) user.get("account");
        ExportTable table = new ExportTable();
        table.setAccount(account);
        table.setTableName("结算单");
        boolean flag = saleMoneyService.exportSaleMoney(table);

        response.addHeader("Access-Control-Expose-Headers","exportFlag");
        response.addHeader("exportFlag",String.valueOf(flag));

        MyExcelUtils.exportExcel(list,
                "结算单",
                "结算单列表",
                SaleMoneyDTO.class,
                "结算单"+time+".xls",response);
    }
}

